package com.gtxd.utils.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.gtxd.utils.AjaxUtil;
import com.gtxd.utils.R;


/**
 * 统一异常处理类
 * 
 * @author YC 2017年9月1日 下午1:18:02
 */
public class MyExceptionHandler implements HandlerExceptionResolver {

	/**
	 * YC 2017年9月1日 下午1:18:02
	 * <p>
	 * Title: resolveException
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param httpservletrequest
	 * @param httpservletresponse
	 * @param obj
	 * @param exception
	 * @return
	 * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      java.lang.Exception)
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse,
			Object obj, Exception exception) {
		if (exception instanceof DataIntegrityViolationException)
			AjaxUtil.ajaxResponse(httpservletresponse, R.error("数据库异常"));
		else
			AjaxUtil.ajaxResponse(httpservletresponse, R.error());
		return null;
	}

}
